package com.example.checkboxdemo.slice;

import com.example.checkboxdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Checkbox;
import ohos.agp.utils.Color;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 获取Checkbox组件实例对象
        Checkbox checkbox1 = (Checkbox) findComponentById(ResourceTable.Id_check_box_1);
        Checkbox checkbox2 = (Checkbox) findComponentById(ResourceTable.Id_check_box_2);
        Checkbox checkbox3 = (Checkbox) findComponentById(ResourceTable.Id_check_box_3);
        // 设置按钮显示的文本
        checkbox1.setText("足球");
        checkbox2.setText("篮球");
        checkbox3.setText("乒乓球");
        // 设置按钮是否选中
        checkbox1.setChecked(true);
        // 设置选中状态的文本颜色
        checkbox1.setTextColorOn(Color.RED);
        // 设置未选中状态的文本颜色
        checkbox1.setTextColorOff(Color.GRAY);
        // 添加选中状态变化的监听
        checkbox1.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                // 选中状态变化的回调
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
